package com.lt.suangua.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.google.android.material.tabs.TabLayout;
import com.hjq.http.EasyConfig;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lt.suangua.R;
import com.lt.suangua.app.TitleBarFragment;
import com.lt.suangua.http.api.HomeApi;
import com.lt.suangua.http.api.LoginApi;
import com.lt.suangua.http.glide.GlideApp;
import com.lt.suangua.http.glide.GlideRoundImage;
import com.lt.suangua.http.model.HttpData;
import com.lt.suangua.ui.activity.BrowserActivity;
import com.lt.suangua.ui.activity.HomeActivity;
import com.lt.suangua.ui.adapter.ImageAdapter;
import com.lt.suangua.ui.adapter.MyPagerAdapter;
import com.lt.suangua.ui.adapter.TabAdapter;
import com.lt.suangua.utils.GlideUtils;
import com.lt.suangua.widget.XCollapsingToolbarLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import okhttp3.Call;

public class NewHomeFragment extends TitleBarFragment<HomeActivity> {
    TextView tv_home;
    Banner banner;
    TabLayout tab_layout1;
    ViewPager leave_viewpager;
    ImageView image_one;
    ImageView image_two;
    ImageView image_three;
    SmartRefreshLayout refreshLayout;

    public static NewHomeFragment newInstance() {
        return new NewHomeFragment();
    }

    @Override
    public boolean isStatusBarEnabled() {
        // 使用沉浸式状态栏
        return !super.isStatusBarEnabled();
    }

//    @Override
//    public boolean isStatusBarDarkFont() {
//        return mCollapsingToolbarLayout.isScrimsShown();
//    }


    @Override
    protected int getLayoutId() {
        return R.layout.home_fragment2;
    }

    @Override
    protected void initView() {
        banner = findViewById(R.id.banner);
        tab_layout1 = findViewById(R.id.tab_layout1);
        leave_viewpager = findViewById(R.id.leave_viewpager);
        image_one = findViewById(R.id.image_one);
        image_two = findViewById(R.id.image_two);
        image_three = findViewById(R.id.image_three);
        refreshLayout = findViewById(R.id.refreshLayout);
        setOnClickListener(R.id.image_one, R.id.image_two, R.id.image_three);
        initListener();
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.image_one) {
            List<HomeApi.Recomm> recomm = mData.recomm;
            if(2==recomm.get(0).linktype){
                HomeActivity activity = (HomeActivity) getActivity();
                activity.switchFragment(1);
                activity.mPublishFragment.suantype=recomm.get(0).suantype;
                activity.mPublishFragment.initData();
            }else{
                BrowserActivity.start(getActivity(), recomm.get(0).url);
            }

        } else if (view.getId() == R.id.image_two) {
            List<HomeApi.Recomm> recomm = mData.recomm;
            if(2==recomm.get(1).linktype){
                HomeActivity activity = (HomeActivity) getActivity();
                activity.switchFragment(1);
                activity.mPublishFragment.suantype=recomm.get(1).suantype;
                activity.mPublishFragment.initData();
            }else{
                BrowserActivity.start(getActivity(), recomm.get(1).url);
            }

        } else if (view.getId() == R.id.image_three) {
            List<HomeApi.Recomm> recomm = mData.recomm;
            if (2 == recomm.get(2).linktype) {
                HomeActivity activity = (HomeActivity) getActivity();
                activity.switchFragment(1);
                activity.mPublishFragment.suantype=recomm.get(2).suantype;
            } else {
                BrowserActivity.start(getActivity(), recomm.get(2).url);
            }
        }
    }

    void initListener() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getData();
            }
        });
    }

    @Override
    protected void initData() {
        getData();
    }

    public void getData() {
        EasyHttp.post(this)
                .api(new HomeApi())
                .request(new HttpCallback<HttpData<HomeApi.Bean>>(this) {

                    @Override
                    public void onStart(Call call) {

                    }

                    @Override
                    public void onEnd(Call call) {
                    }

                    @Override
                    public void onSucceed(HttpData<HomeApi.Bean> data) {
                        Log.d("shuju", data.getData().banner.size() + ",");
                        refreshLayout.finishRefresh(200);
                        setViewData(data.getData());
                    }

                    @Override
                    public void onFail(Exception e) {
                        super.onFail(e);
                        refreshLayout.finishRefresh(200);
                        Log.d("shuju", e.getMessage());
                    }
                });
    }

    HomeApi.Bean mData;

    public void setViewData(HomeApi.Bean data) {
        this.mData = data;
        banner.addBannerLifecycleObserver(this)//添加生命周期观察者
                .setAdapter(new ImageAdapter(data.banner))
                .setIndicator(new CircleIndicator(getActivity()));
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(Object data, int position) {
                List<HomeApi.Banner> bannerList = mData.banner;
                BrowserActivity.start(getActivity(), bannerList.get(position).url);
            }
        });
        List<HomeApi.Recomm> recomm = data.recomm;
        if (recomm != null ) {
            if(recomm.size()==1){
                image_one.setVisibility(View.VISIBLE);
                image_two.setVisibility(View.GONE);
                image_three.setVisibility(View.GONE);
                GlideUtils.loadCornerImage(getActivity(),recomm.get(0).pic,image_one,15);

            }else if(recomm.size()==2){
                image_one.setVisibility(View.VISIBLE);
                image_two.setVisibility(View.VISIBLE);
                image_three.setVisibility(View.GONE);
                GlideUtils.loadCornerImage(getActivity(),recomm.get(1).pic,image_two,15);
            }else if(recomm.size()==3){
                image_one.setVisibility(View.VISIBLE);
                image_two.setVisibility(View.VISIBLE);
                image_three.setVisibility(View.VISIBLE);
                GlideUtils.loadCornerImage(getActivity(),recomm.get(0).pic,image_one,15);
                GlideUtils.loadCornerImage(getActivity(),recomm.get(1).pic,image_two,15);
                GlideUtils.loadCornerImage(getActivity(),recomm.get(2).pic,image_three,15);

            }

        }


        List<Fragment> fragments = new ArrayList<>(); //
        List<HomeApi.Cate> cates = data.articleArea.cates;
        List<String> titles = new ArrayList<>();
        if (cates != null && cates.size() > 0) {
            for (int i = 0; i < cates.size(); i++) {
                //tab_layout1.addTab(tab_layout1.newTab().setText(cates.get(i).name));
                ArticleFragment articleFragment = new ArticleFragment();
                Bundle bundle = new Bundle();
                bundle.putSerializable("articleArea", data.articleArea);
                bundle.putString("cate", cates.get(i).cate);
                articleFragment.setArguments(bundle);
                fragments.add(articleFragment);
//                tab_layout1.addTab(tab_layout1.newTab().setText("haha" + 1));

                titles.add(cates.get(i).name);
            }


            MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager(), fragments, titles);
            leave_viewpager.setAdapter(adapter);              //绑定adapter
            tab_layout1.setupWithViewPager(leave_viewpager);    //绑定viewPager

        }

    }
}
